package com.ciwong.epaper.modules.epaper.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.ListenSpeakExAminAtion;
import com.ciwong.epaper.modules.epaper.bean.ListenSpeakExaminBean;
import com.ciwong.epaper.modules.epaper.ui.ListenSpeakChooseWorkActivity;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.mobilelib.i.ViewOnClickListener;

/**
 * @description: 听说模考作业选择适配器
 * @author: wyk
 * @time: 2021/5/20 11:02
 */
public class ListenSpeakChooseWorkAdapter extends BaseAdapter {

    private ListenSpeakExaminBean listenSpeakExaminBean = new ListenSpeakExaminBean();

    private Context mContext;

    private String versionId;

    private String tag;

    public ListenSpeakChooseWorkAdapter(ListenSpeakExaminBean listenSpeakExaminBean, Context mContext, String versionId, String tag) {
        this.listenSpeakExaminBean = listenSpeakExaminBean;
        this.mContext = mContext;
        this.versionId = versionId;
        this.tag = tag;
    }

    @Override
    public int getCount() {
        return null == listenSpeakExaminBean.getListenSpeakExAminAtion() ? 0 : listenSpeakExaminBean.getListenSpeakExAminAtion().getParts().size();
    }

    @Override
    public Object getItem(int i) {
        return null == listenSpeakExaminBean.getListenSpeakExAminAtion() ? null : listenSpeakExaminBean.getListenSpeakExAminAtion().getParts().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (null == view) {
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_listen_speak_choose_work_list_item, viewGroup, false);
        }
        final ListenSpeakExAminAtion listenSpeakExAminAtion = listenSpeakExaminBean.getListenSpeakExAminAtion();
        int topicCount = listenSpeakExAminAtion.getParts().size();
        final CheckBox listen_speak_choose_work_content = (CheckBox) view.findViewById(R.id.listen_speak_choose_work_content);
        TextView choose_listen_speak_work_name_text = (TextView) view.findViewById(R.id.choose_listen_speak_work_name_text);
        ImageView check_subject = (ImageView) view.findViewById(R.id.check_subject);
        //设置题目标题
        choose_listen_speak_work_name_text.setText(listenSpeakExAminAtion.getParts().get(i).getModule_type_name());
        /*if (listenSpeakExaminBean.getCheckCount() == topicCount) {
            listen_speak_choose_work_content.setChecked(true);
        } else if (listenSpeakExaminBean.getCheckCount() == 0) {
            listen_speak_choose_work_content.setChecked(false);
        } else {*/
        if (null == listenSpeakExAminAtion.getParts().get(i).getChecked() || listenSpeakExAminAtion.getParts().get(i).getChecked()) {
            listen_speak_choose_work_content.setChecked(true);
        } else {
            listen_speak_choose_work_content.setChecked(false);
        }


        listen_speak_choose_work_content.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                if (b) {
                    listen_speak_choose_work_content.setChecked(true);
                    listenSpeakExAminAtion.getParts().get(i).setChecked(true);
                    listenSpeakExaminBean.setCheckCount(listenSpeakExaminBean.getCheckCount() + 1);
                    if (mContext instanceof ListenSpeakChooseWorkActivity) {
                        ((ListenSpeakChooseWorkActivity) mContext).setCheckStatus();
                    }

                } else {
                    listen_speak_choose_work_content.setChecked(false);
                    listenSpeakExAminAtion.getParts().get(i).setChecked(false);
                    listenSpeakExaminBean.setCheckCount(listenSpeakExaminBean.getCheckCount() - 1);
                    if (mContext instanceof ListenSpeakChooseWorkActivity) {
                        ((ListenSpeakChooseWorkActivity) mContext).setCheckStatus();
                    }
                }
            }
        });

        check_subject.setOnClickListener(new ViewOnClickListener() {
            @Override
            public void avertRepeatOnClick(View view) {
                EpaperJumpManager.jumpToListenSpeakTopic(R.id.go_back, i, versionId, listenSpeakExaminBean.getListenSpeakExAminAtion().getParts().get(i).getModule_type_name(), tag, (Activity) mContext);
            }
        });
        return view;
    }
}
